package clubhouse.error;

public class TransmissionError {
 
	private int errorFactor = 10;
	
	public TransmissionError() {
	}
	
	public void setErrorFactor(int error) {
		this.errorFactor = error;
	}
	
	private double euclideanDist(double x1, double y1, double x2, double y2){
		return(Math.sqrt((x2-x1)*(x2-x1) + (y2-y1)*(y2-y1)));
	}
	
	public double[] applyError(double x_robot, double y_robot, double x_node, double y_node) {
		double[] result = new double [2];
		double distance;
				
		distance = euclideanDist(x_robot,y_robot,x_node,y_node);
		do {
			result[0] = x_node + (Math.random()*2-1)*distance/this.errorFactor;	//Random uniform Distribution
			result[1] = y_node + (Math.random()*2-1)*distance/this.errorFactor;
			} while (euclideanDist(x_node,y_node,result[0],result[1]) > distance/this.errorFactor);
		return result;
	}
	
	public double[] applyErrorFromPool(double err, double x_robot, double y_robot, double x_node, double y_node) {
		double[] result = new double [2];
		double distance;
				
		distance = euclideanDist(x_robot,y_robot,x_node,y_node);
		//do {
			result[0] = x_node + err*distance/this.errorFactor;	//Random uniform Distribution
			result[1] = y_node + err*distance/this.errorFactor;
		//	} while (euclideanDist(x_node,y_node,result[0],result[1]) > distance/TransmissionError.errorFactor);
		return result;
	}
}
